package com.lam.schedule.entity;

import java.io.Serializable;
import java.util.Date;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.lam.common.base.domain.BaseEntity;
import com.lam.common.components.dict.annotation.Dict;
import com.lam.common.components.easyexcel.converter.DictConverter;
import com.lam.common.constant.CommonConstant;
import com.lam.common.utils.DateUtil;
import com.lam.common.utils.NumberUtil;
import com.lam.common.validate.annotation.DictValid;
import com.lam.schedule.common.constant.ScheduleConstant;

/**
 * 调度日志对象 schedule_job_log
 * 
 * @author admin
 * @date 2022-07-18
 */
@ColumnWidth(30)
@TableName("schedule_job_log")
public class ScheduleJobLog implements BaseEntity, Serializable{

	private static final long serialVersionUID = 1L;

	/** 主键 */
    @ExcelIgnore
    @TableId(type = IdType.AUTO)
    private String id;
    
    /** 任务ID */
    @ExcelProperty("任务ID")
    private String scheduleJobId;
    
    /** 调度器名称 */
    @ExcelProperty("调度器名称")
    private String schedName;
    /** 实例名 */
    @ExcelProperty("实例名")
    private String instanceName;
    /** 实例IP */
    @ExcelProperty("实例IP")
    private String instanceIp;
    /** 实例id */
    @ExcelProperty("实例id")
    private String instanceId;
    
    /** 任务名称 */
    @ExcelProperty("任务名称")
    private String jobName;
    
    /** 任务分组 */
    @ExcelProperty("任务分组")
    private String jobGroup;
    
    /** 状态（1正常 0暂停） */
    @ExcelProperty(value = "状态", converter = DictConverter.class)
    @Dict(dictCode = "job_status")
    private Integer status;
    
    /** con表达式 */
    @ExcelProperty("con表达式")
    private String cronExpression;
    
    /** 表达式描述 */
    @ExcelProperty("表达式描述")
    private String cronExpDesc;
    
    /** 执行类 */
    @ExcelProperty("执行类")
    private String exeClass;
    
    /** 执行方法 */
    @ExcelProperty("执行方法")
    private String exeMethod;
    
    /** 执行参数 */
    @ExcelProperty("执行参数")
    private String exeParam;
    
    /** 并发类型(1-允许并发 0-串行执行) */
    @ExcelProperty(value = "并发类型", converter = DictConverter.class)
    @Dict(dictCode = "concurrent_type")
    private Integer concurrentType;
    
    /** 执行错误策略（1-立即执行 2-执行一次 3-放弃执行） */
    @ExcelProperty(value = "执行错误策略", converter = DictConverter.class)
    @Dict(dictCode = "misfire_policy")
    private Integer misfirePolicy;
    
    /** 记录调度日志(1-是, 0-否) */
    @ExcelProperty(value = "记录日志", converter = DictConverter.class)
    @DictValid(message = "记录日志值无效")
    @Dict(dictCode = "yn10")
    private Integer collectLog;
    
    /** 触发方式（1-定时器触发 2-手动触发 3-程序调用） */
    @ExcelProperty(value = "触发方式", converter = DictConverter.class)
    @Dict(dictCode = "trigger_type")
    private Integer triggerType;
    
    /** 开始执行时间 */
    @ExcelProperty("开始执行时间")
    @JsonFormat(pattern = DateUtil.PATTERN_DEFAULT)
    private Date exeStartTime;
    
    /** 执行结束时间 */
    @ExcelProperty("执行结束时间")
    @JsonFormat(pattern = DateUtil.PATTERN_DEFAULT)
    private Date exeEndTime;
    
    /** 执行耗时,毫秒 */
    @ExcelProperty("执行耗时,毫秒")
    private Long exeCost;
    
    /** 执行结果(1-成功 0-失败) */
    @ExcelProperty(value = "执行结果", converter = DictConverter.class)
    @Dict(dictCode = "exe_status_type")
    private Integer exeStatus;
    
    /** 执行结果信息 */
    @ExcelProperty("执行结果信息")
    private String exeMsg;
    
    /** 创建人 */
    @ExcelIgnore
    private String createBy;
    
    /** 创建时间 */
    @ExcelIgnore
    @JsonFormat(pattern = DateUtil.PATTERN_DEFAULT)
    private Date createTime;
    
    /** 备注信息 */
    @ExcelProperty("备注信息")
    private String remark;
    
    public ScheduleJobLog() {}
    
    public ScheduleJobLog(ScheduleJob job, int triggerType) {
		this.scheduleJobId = job.getId();
		this.jobName = job.getJobName();
		this.jobGroup = job.getJobGroup();
		this.status = job.getStatus();
		this.cronExpression = job.getCronExpression();
		this.cronExpDesc = job.getCronExpDesc();
		this.exeClass = job.getExeClass();
		this.exeMethod = job.getExeMethod();
		this.exeParam = job.getExeParam();
		this.concurrentType = job.getConcurrentType();
		this.misfirePolicy = job.getMisfirePolicy();
		this.collectLog = job.getCollectLog();
		this.triggerType = triggerType;
	}
    
    public ScheduleJobLog start() {
    	exeStartTime = new Date();
    	exeStatus = ScheduleConstant.EXE_STATUS_GOING;
    	exeMsg = "正在执行";
    	return this;
    }
    
    public boolean collectLog() {
    	return NumberUtil.eq(CommonConstant.YES1, collectLog);
    }
    
    /**
     * 计算耗时
     * @return
     */
    public ScheduleJobLog calcCost() {
    	exeEndTime = new Date();
    	exeCost = exeEndTime.getTime() - exeStartTime.getTime();
    	return this;
    }
    
    public String getExeCostText() {
		if(exeCost != null) {
			return DateUtil.convertTimeLong(exeCost);
		}
		return null;
	}

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    } 

    public void setScheduleJobId(String scheduleJobId) {
        this.scheduleJobId = scheduleJobId;
    }

    public String getScheduleJobId() {
        return scheduleJobId;
    }    
    
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getJobName() {
        return jobName;
    }    
    
    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }

    public String getJobGroup() {
        return jobGroup;
    }    
    
    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getStatus() {
        return status;
    }    
    
    public void setCronExpression(String cronExpression) {
        this.cronExpression = cronExpression;
    }

    public String getCronExpression() {
        return cronExpression;
    }    
    
    public void setCronExpDesc(String cronExpDesc) {
        this.cronExpDesc = cronExpDesc;
    }

    public String getCronExpDesc() {
        return cronExpDesc;
    }    
    
    public void setExeClass(String exeClass) {
        this.exeClass = exeClass;
    }

    public String getExeClass() {
        return exeClass;
    }    
    
    public void setExeMethod(String exeMethod) {
        this.exeMethod = exeMethod;
    }

    public String getExeMethod() {
        return exeMethod;
    }    
    
    public void setExeParam(String exeParam) {
        this.exeParam = exeParam;
    }

    public String getExeParam() {
        return exeParam;
    }    
    
    public void setConcurrentType(Integer concurrentType) {
        this.concurrentType = concurrentType;
    }

    public Integer getConcurrentType() {
        return concurrentType;
    }    
    
    public void setMisfirePolicy(Integer misfirePolicy) {
        this.misfirePolicy = misfirePolicy;
    }

    public Integer getMisfirePolicy() {
        return misfirePolicy;
    }    
    
    public void setExeStartTime(Date exeStartTime) {
        this.exeStartTime = exeStartTime;
    }

    public Date getExeStartTime() {
        return exeStartTime;
    }    
    
    public void setExeEndTime(Date exeEndTime) {
        this.exeEndTime = exeEndTime;
    }

    public Date getExeEndTime() {
        return exeEndTime;
    }    
    
    public void setExeCost(Long exeCost) {
        this.exeCost = exeCost;
    }

    public Long getExeCost() {
        return exeCost;
    }    
    
    public void setExeStatus(Integer exeStatus) {
        this.exeStatus = exeStatus;
    }

    public Integer getExeStatus() {
        return exeStatus;
    }    
    
    public void setExeMsg(String exeMsg) {
        this.exeMsg = exeMsg;
    }

    public String getExeMsg() {
        return exeMsg;
    }    
    
    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getCreateBy() {
        return createBy;
    }    
    
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getCreateTime() {
        return createTime;
    }    
    
    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getRemark() {
        return remark;
    }

	@Override
	public String getUpdateBy() {
		return null;
	}

	@Override
	public void setUpdateBy(String updateBy) {
		
	}

	@Override
	public Date getUpdateTime() {
		return null;
	}

	@Override
	public void setUpdateTime(Date updateTime) {
	}

	public Integer getTriggerType() {
		return triggerType;
	}

	public void setTriggerType(Integer triggerType) {
		this.triggerType = triggerType;
	}

	public String getInstanceName() {
		return instanceName;
	}

	public void setInstanceName(String instanceName) {
		this.instanceName = instanceName;
	}

	public String getInstanceIp() {
		return instanceIp;
	}

	public void setInstanceIp(String instanceIp) {
		this.instanceIp = instanceIp;
	}

	public String getInstanceId() {
		return instanceId;
	}

	public void setInstanceId(String instanceId) {
		this.instanceId = instanceId;
	}

	public String getSchedName() {
		return schedName;
	}

	public void setSchedName(String schedName) {
		this.schedName = schedName;
	}

	public Integer getCollectLog() {
		return collectLog;
	}

	public void setCollectLog(Integer collectLog) {
		this.collectLog = collectLog;
	}    
    

}
